package amtf

import (
	"fmt"
	"strconv"
	"time"
)

func A字符串转uint(str string) (res uint) {
	num, err := strconv.ParseUint(str, 10, 64)
	if err != nil {
		fmt.Printf("转换出错: %v\n", err)
		// return
	}
	// 将 uint64 转换为 uint 类型
	res = uint(num)
	return
}

func A转string(value any) string {
	switch v := value.(type) {
	case string:
		return v
	case int:
		return strconv.Itoa(v)
	case int8:
		return strconv.Itoa(int(v))
	case int16:
		return strconv.Itoa(int(v))
	case int32:
		return strconv.Itoa(int(v))
	case int64:
		return strconv.FormatInt(v, 10)
	case uint:
		return strconv.FormatUint(uint64(v), 10)
	case uint8:
		return strconv.FormatUint(uint64(v), 10)
	case uint16:
		return strconv.FormatUint(uint64(v), 10)
	case uint32:
		return strconv.FormatUint(uint64(v), 10)
	case uint64:
		return strconv.FormatUint(v, 10)
	case float32:
		return strconv.FormatFloat(float64(v), 'f', -1, 32)
	case float64:
		return strconv.FormatFloat(v, 'f', -1, 64)
	case bool:
		return strconv.FormatBool(v)
	case time.Time:
		return v.Format(time.RFC3339)
	default:
		return fmt.Sprintf("%v", v)
	}
}

func A转uint2(value interface{}) uint {
	// fmt.Printf("原始值: %+v, 类型: %T\n", value, value)
	switch v := value.(type) {
	case uint:
		return v
	case uint64:
		return uint(v)
	case int:
		if v < 0 {
			return 0
		}
		return uint(v)
	case int64:
		if v < 0 {
			return 0
		}
		return uint(v)
	case float64:
		if v < 0 || v != float64(int64(v)) {
			return 0
		}
		return uint(v)
	default:
		return 0
	}
}

func A转uint(value interface{}) (uint, bool) {
	// fmt.Printf("原始值: %+v, 类型: %T\n", value, value)
	switch v := value.(type) {
	case uint:
		return v, true
	case uint64:
		return uint(v), true
	case int:
		if v < 0 {
			return 0, false
		}
		return uint(v), true
	case int64:
		if v < 0 {
			return 0, false
		}
		return uint(v), true
	case float64:
		if v < 0 || v != float64(int64(v)) {
			return 0, false
		}
		return uint(v), true
	default:
		return 0, false
	}
}
